package com.fametal.ui.adapter;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fametal.R;
import com.fametal.ui.model.Phone;

public class PhoneNumAdapter extends BaseAdapter{

	private List<Phone> mList;
	private Context mContext;
	private LayoutInflater layoutInflater;
	
	public PhoneNumAdapter(Context context, List<Phone> list) {
		this.mContext = context;
		layoutInflater = LayoutInflater.from(context);
		mList = list;
	}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = layoutInflater.inflate(R.layout.phone_list_item, null);
		}
		final Phone phone = mList.get(position);
		TextView name = (TextView) convertView.findViewById(R.id.name);
		TextView phoneNum = (TextView) convertView.findViewById(R.id.phonenum);
		TextView edit = (TextView) convertView.findViewById(R.id.edit);
		edit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mList.remove(phone);
				notifyDataSetChanged();
				listener.remove(phone);
			}
		});
		name.setText(phone.name);
		phoneNum.setText(phone.number);
		return convertView;
	}
	
	private RemoveListener listener;
	public void setRemoveListener(RemoveListener listener) {
		this.listener = listener;
	}
	public interface RemoveListener{
		public void remove(Phone phone);
	}

}
